home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / cmplib_s.lha / cmplib_src / $normvarocc1.P < prev    next >
Text File  |  1990-07-26  |  6KB  |  158 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $normvarocc1.P */
  25.  
  26. /* ********************************************************************** 
  27. $normvarocc1_export([$normalize_var_occ/7]).
  28.  
  29. $normvarocc1_use($computil1,[_,_,_,_,_,$union_varsets/3,_,
  30.            $diff_sets/3,$intersect_sets/3,_,_,_,_,_,_,_,_,_,_,_]).
  31. $normvarocc1_use($aux1,[_,_,_,_,_,_,_,_,$logical_or/3]).
  32. $normvarocc1_use($blist,[_,_,$member1/2]).
  33. $normvarocc1_use($listutil1,[_,_,_,_,_,_,_,$closetail/1]).
  34. $normvarocc1_use($prococcbody1,[_,$list_pvars/2]).
  35. ********************************************************************** */
  36.  
  37. /*  "$normalize_var_occ" normalizes variable occurrences in disjunctive
  38.      paths, so that every variable has a unique first/subsequent occurrence,
  39.      irrespective of the disjunctive path considered. The algorithm is as
  40.      follows:  If u0, u1, u2 and u3 be the variable occurring in goals at
  41.      the points specified below, and V1 and V2 the set of variables that
  42.      are added to normalize occurrences, then
  43.  
  44.         V1 = (u2 - u1) * (u3 - u0), and
  45.  
  46.         V2 = (u1 - u2) * (u3 - u0),  where  `*' = set intersection,
  47.                             `-' = set difference.
  48.  
  49.                +--------+ +--------+
  50.               -----+   u1   +-+   V1   +---- 
  51.     +------+     /       +--------+ +--------+     \       +------+
  52. --------|  u0  |----<                         >------+  u3  +-->
  53.     +------+     \     +--------+ +--------+     /       +------+
  54.               -----+   u2   +-+   V2   +----
  55.                +--------+ +--------+
  56.                                     */
  57.  
  58. $norm_newaddlist(L1,L2,L3,L) :-
  59.     $diff_sets(L2,L1,D1), $intersect_sets(D1,L3,L).
  60.  
  61.  
  62. $normalize_var_occ(nil,nil,[],[],[],[],0) :- !.
  63. $normalize_var_occ(Body1,Body2,LinLeft,LinRight,Lout,Used,Chg) :-
  64.     Body1 = and(A1,P,B1),
  65.     !,
  66.     $norm_pvars_seen(A1,PVars1), $union_varsets(LinLeft,PVars1,LinLeft1),
  67.     $normalize_var_occ(B1,B2,LinLeft1,LinRight,L1,Used1,Chg1),
  68.     $normalize_var_occ(A1,A2,LinLeft,L1,Lout,Used2,Chg2),
  69.     $union_varsets(Used1,Used2,Used),
  70.     $logical_or(Chg1,Chg2,Chg),
  71.     ((Chg =:= 0, Body2 = Body1) ;
  72.      (Chg =\= 0, Body2 = and(A2,P,B2))
  73.     ).
  74. $normalize_var_occ(Body1,Body2,LinLeft,LinRight,Lout,Used,Chg) :-
  75.     Body1 = if_then_else(T,P,A1,B1),
  76.     !,
  77.     $normalize_varocc_disj(A1,B1,LinLeft,LinRight,A2,B2,Used1,Used2,Chg),
  78.     ((Chg =:= 0, Body2 = Body1) ;
  79.      (Chg =\= 0, Body2 = if_then_else(T,P,A2,B2))
  80.     ),
  81.     $union_varsets(Used1,Used2,Used0),
  82.     $norm_pvars_seen(T,PvarsT), $union_varsets(PvarsT,Used0,Used),
  83.     $union_varsets(Used,LinRight,Lout),
  84.     $intersect_sets(Used1,Used2,PVars0),
  85.     $union_varsets(PvarsT,PVars0,PVars),
  86.     $member1(pvars(PVars),P),
  87.     $closetail(P).
  88. $normalize_var_occ(Body1,Body2,LinLeft,LinRight,Lout,Used,Chg) :-
  89.     Body1 = or(A1,P,B1),
  90.     !,
  91.     $normalize_varocc_disj(A1,B1,LinLeft,LinRight,A2,B2,Used1,Used2,Chg),
  92.     ((Chg =:= 0, Body2 = Body1) ;
  93.      (Chg =\= 0, Body2 = or(A2,P,B2))
  94.     ),
  95.     $union_varsets(Used1,Used2,Used),
  96.     $union_varsets(Used,LinRight,Lout),
  97.     $intersect_sets(Used1,Used2,PVars),
  98.     $member1(pvars(PVars),P),
  99.     $closetail(P).
  100. $normalize_var_occ(not(A1,P),not(A2,P),LinLeft,LinRight,Lout,Lused,Chg) :-
  101.     !,
  102.     $normalize_var_occ(A1,A2,LinLeft,LinRight,Lout,Lused,Chg).
  103. $normalize_var_occ('_call'(P,Args,X),'_call'(P,Args,X),_,LinRight,Lout,L,0) :-
  104.     !,
  105.     $list_pvars(Args,L),
  106.     $union_varsets(L,LinRight,Lout),
  107.     $member1(pvars(L),X).
  108. $normalize_var_occ(C,C,_,L,L,[],0).
  109.  
  110. $normalize_varocc_disj(A1,B1,LinLeft,LinRight,A2,B2,Used1,Used2,Chg0) :-
  111.     $normalize_var_occ(A1,A1a,LinLeft,LinRight,L1,Used1a,Chg1),
  112.     $normalize_var_occ(B1,B1a,LinLeft,LinRight,L2,Used2a,Chg2),
  113.     $logical_or(Chg1,Chg2,Chg3),
  114.     $diff_sets(LinRight,LinLeft,LinNew),
  115.     $norm_newaddlist(Used1a,Used2a,LinNew,NewAdd1),
  116.     $norm_newaddlist(Used2a,Used1a,LinNew,NewAdd2),
  117.     ((NewAdd1 = [], NewAdd2 = []) ->
  118.           (A2 = A1a, B2 = B1a,
  119.           Used1 = Used1a, Used2 = Used2a,
  120.           Chg4 = 0
  121.          ) ;
  122.          ($normalize1(A1a,NewAdd1,A2),
  123.           $normalize1(B1a,NewAdd2,B2),
  124.           $union_varsets(Used1a,NewAdd1,Used1),
  125.           $union_varsets(Used2a,NewAdd2,Used2),
  126.           Body2 = if_then_else(T,P,A2,B2),
  127.           Chg4 = 1
  128.          )
  129.     ),
  130.     $logical_or(Chg3,Chg4,Chg0).
  131.  
  132. $norm_pvars_seen(and(C1,_,C2),L) :-
  133.     $norm_pvars_seen(C1,L1),
  134.     $norm_pvars_seen(C2,L2),
  135.     $union_varsets(L1,L2,L).
  136. $norm_pvars_seen(or(C1,_,C2),L) :-
  137.     $norm_pvars_seen(C1,L1),
  138.     $norm_pvars_seen(C2,L2),
  139.     $union_varsets(L1,L2,L).
  140. $norm_pvars_seen(if_then_else(T,_,G1,G2),L) :-
  141.     $norm_pvars_seen(T,L1),
  142.     $norm_pvars_seen(G1,L2),
  143.     $norm_pvars_seen(G2,L3),
  144.     $union_varsets(L2,L3,L4),
  145.     $union_varsets(L1,L4,L).
  146. $norm_pvars_seen(not(C,_),L) :- $norm_pvars_seen(C,L).
  147. $norm_pvars_seen('_call'(_,Args,_),L) :- $list_pvars(Args,L).
  148.  
  149. :- index($normalize1,3,2).
  150.  
  151. $normalize1(A,[],A).
  152. $normalize1(A1,[v(Vid,vrec(T,_,L,M))|VRest],A2) :-
  153.    Prag1 = vrec(T,_,L,M), Prag2 = vrec(T,_,L,M),
  154.    A3 = and(A1,_,'_call'('=',[v(Vid,Prag1),v(Vid,Prag2)],_)),
  155.    $normalize1(A3,VRest,A2).
  156.  
  157. /* end normvarocc1.P *********************************/
  158.